package com.ruoyi.service.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.service.api.RemoteServiceSortService;
import com.ruoyi.service.api.domain.CheServiceSort;
import com.ruoyi.shop.api.RemoteShopService;
import com.ruoyi.shop.api.domain.CheShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.service.mapper.CheServiceMapper;
import com.ruoyi.service.api.domain.CheService;
import com.ruoyi.service.service.ICheServiceService;

/**
 * 服务项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@Service
public class CheServiceServiceImpl implements ICheServiceService 
{
    @Autowired
    private CheServiceMapper cheServiceMapper;
    @Autowired
    private RemoteShopService remoteShopService;
    @Autowired
    private RemoteServiceSortService remoteServiceSortService;

    /**
     * 查询服务项目
     * 
     * @param id 服务项目主键
     * @return 服务项目
     */
    @Override
    public CheService selectCheServiceById(Long id)
    {
        CheService cheService = cheServiceMapper.selectCheServiceById(id);
        Long shopId = cheService.getShopId();
        AjaxResult info = remoteShopService.getInfo(shopId, null);
        Object o = info.get(AjaxResult.DATA_TAG);
        if (o==null){
            return cheService;
        }
        CheShop cheShop = JSON.parseObject(JSON.toJSON(o).toString(), CheShop.class);
        cheService.setCheShop(cheShop);
        return cheService;
    }

    /**
     * 查询服务项目列表
     * 
     * @param cheService 服务项目
     * @return 服务项目
     */
    @Override
    public List<CheService> selectCheServiceList(CheService cheService)
    {
        List<CheService> cheServices = cheServiceMapper.selectCheServiceList(cheService);
        cheServices.forEach(e->{
            Long shopId = e.getShopId();
            Long serviceSortId = e.getServiceSortId();
            AjaxResult info = remoteShopService.getInfo(shopId, null);
            Object o = info.get(AjaxResult.DATA_TAG);
            if (o==null){
                return;
            }
            CheShop cheShop = JSON.parseObject(JSON.toJSON(o).toString(), CheShop.class);
            e.setCheShop(cheShop);
            AjaxResult info1 = remoteServiceSortService.getInfo(serviceSortId, null);
            Object o1 = info1.get(AjaxResult.DATA_TAG);
            if (o1==null){
                return;
            }
            CheServiceSort cheServiceSort = JSON.parseObject(JSON.toJSON(o1).toString(), CheServiceSort.class);
            e.setCheServiceSort(cheServiceSort);
        });
        return cheServices;

    }

    @Override
    public List<CheService> selectListByIds(Long[] ids) {
        if(ids == null || ids.length == 0)
            return null;
//        System.out.println("ids:"+ Arrays.toString(ids));

        List<CheService> cheServices = cheServiceMapper.selectListByIds(ids);
        System.out.println(cheServices);
        return cheServices;
    }

    @Override
    public List<CheService> selectListByShopId(Long shopId) {
        if(shopId == null){
            return null;
        }

        return cheServiceMapper.selectListByShopId(shopId);

    }

    /**
     * 新增服务项目
     * 
     * @param cheService 服务项目
     * @return 结果
     */
    @Override
    public int insertCheService(CheService cheService)
    {
        cheService.setCreateTime(DateUtils.getNowDate());
        return cheServiceMapper.insertCheService(cheService);
    }

    /**
     * 修改服务项目
     * 
     * @param cheService 服务项目
     * @return 结果
     */
    @Override
    public int updateCheService(CheService cheService)
    {
        return cheServiceMapper.updateCheService(cheService);
    }

    /**
     * 批量删除服务项目
     * 
     * @param ids 需要删除的服务项目主键
     * @return 结果
     */
    @Override
    public int deleteCheServiceByIds(Long[] ids)
    {
        return cheServiceMapper.deleteCheServiceByIds(ids);
    }

    /**
     * 删除服务项目信息
     * 
     * @param id 服务项目主键
     * @return 结果
     */
    @Override
    public int deleteCheServiceById(Long id)
    {
        return cheServiceMapper.deleteCheServiceById(id);
    }
}
